
import axios from 'axios'
import { ref } from 'vue'
import { useUserStore } from '@fl/store/modules/user'
import { getRegionList, getUserAreas } from '@fl/api/joint-api'
import { useJointStore } from '@fl/store/modules/joint'
import { useSessionStorage } from '@vueuse/core'
type IArea = {
    code: string
    name: string
    areaList?: IArea[]
}

export function useAreaData() {
    // const treeData = ref<IArea[]>([])
    const areaList = useSessionStorage<IArea[]>('areaList', [])
    const targetProvince = ref<IArea | undefined>({
        name: '湖北省',
        code: '420000000000',
    })
    const userStore = useUserStore()
    // console.log('🚀 ~ generateHeaderMenu ~ userInfo:', userInfo)
    const { userFlag } = userStore.getUserInfo
    const jointCard = useJointStore().getJointCardInfo

    const fetch = async () => {
        let res;
        if (userFlag === '1') {
            res = await getRegionList()

            // treeData.value = res
            areaList.value = res?.map((item: any) => {
                return {
                    name: item.areaName,
                    code: item.areaCode,
                    areaList: item.children?.map((item: any) => {
                        return {
                            name: item.areaName,
                            code: item.areaCode,
                        }
                    })
                }
            })
            // targetProvince.value = res.data.find((item: any) => item.name === '湖北省')
            // if (targetProvince?.value?.areaList) {
            //     areaList.value = targetProvince.value.areaList
            // }
        } else {
            res = await getUserAreas({ appId: Number(jointCard.appId), })
            if (res?.dataScope) {
                const newData = res.dataScope.find((item: any) => item.provinceName === '湖北省')?.cityList.map((item: any) => {
                    return {
                        code: item.cityCode,
                        name: item.cityName,
                        areaList: item.areaList?.filter((item: any) => item.areaCode).map((item: any) => {
                            return {
                                name: item.areaName,
                                code: item.areaCode,
                            }
                        }),
                    }
                })
                // treeData.value = [{ name: '湖北省', code: '420000', areaList: newData }]
                targetProvince.value = { name: '湖北省', code: '420000' }
                areaList.value = newData

            }

        }

    }

    const getAreaName = (code: string) => {
        // 根据code查找区域名称,先拍平数组
        const flatAreaList = areaList.value.reduce<IArea[]>((pre, cur) => {
            pre.push(cur)
            if (cur.areaList && cur.areaList.length) {
                pre.push(...cur.areaList)
            }
            return pre
        }, [])

        const area = flatAreaList.find((item) => {
            return item.code === code
        })

        return area?.name || ''
    }

    onMounted(() => {
        if (areaList.value.length) {
            return
        }
        fetch()
    })

    return { areaList, getAreaName, targetProvince }
}